package com.glela.serial.service.impl;

import com.glela.common.model.ProductTag;
import com.glela.common.util.LinkUtil;
import com.glela.goods.mapper.GoodsMapper;
import com.glela.goods.mapper.GoodsNavigateMapper;
import com.glela.goods.mapper.ShareGoodsMapper;
import com.glela.goods.model.GoodsPrice;
import com.glela.goods.model.GoodsPriceVo;
import com.glela.goods.model.vo.goods.GoodSerialVo;
import com.glela.goods.service.GoodsService;
import com.glela.goods.service.ShareGoodsService;
import com.glela.platform.mapper.BaseRestrictionsMapper;
import com.glela.platform.mapper.UserMapper;
import com.glela.platform.model.BaseRestrictions;
import com.glela.platform.model.User;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.service.BaseRestrictionsService;
import com.glela.platform.service.UserService;
import com.glela.platform.util.DateUtils;
import com.glela.serial.mapper.TitleDiscoveryImageMapper;
import com.glela.serial.mapper.TitleDiscoveryMapper;
import com.glela.serial.mapper.TitleDiscoveryProductMapper;
import com.glela.serial.mapper.TitleDiscoveryTagMapper;
import com.glela.serial.model.DiscoveryTypeVo;
import com.glela.serial.model.TitleDiscovery;
import com.glela.serial.model.TitleDiscoveryImage;
import com.glela.serial.model.TitleDiscoveryProduct;
import com.glela.serial.model.TitleDiscoveryTag;
import com.glela.serial.model.response.ResponseTitleDiscoveryVo;
import com.glela.serial.service.TitleDiscoveryService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;


@Service("titleDiscoveryService")
public class TitleDiscoveryServiceImpl implements TitleDiscoveryService {
	private static Logger logger = LoggerFactory.getLogger(TitleDiscoveryServiceImpl.class);

	@Autowired
	private TitleDiscoveryMapper titleDiscoveryMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private BaseRestrictionsMapper baseRestrictionsMapper;
	@Autowired
	private TitleDiscoveryProductMapper titleDiscoveryProductMapper;
	@Autowired
	private TitleDiscoveryImageMapper titleDiscoveryImageMapper;
	@Autowired
	private TitleDiscoveryTagMapper titleDiscoveryTagMapper;
	@Autowired
	private GoodsMapper goodsMapper;
	@Autowired
	private ShareGoodsMapper shareGoodsMapper;
	@Autowired
	private BaseRestrictionsService baseRestrictionsService;
	@Autowired
	private UserService userService;
	@Autowired
	private ShareGoodsService shareGoodsService;
	@Autowired
	private GoodsNavigateMapper goodsNavigateMapper;
	
	
	@Override
	public List<TitleDiscovery> selectDiscoveryPageByType(TitleDiscovery request) {
		List<TitleDiscovery> pageList = titleDiscoveryMapper.queryAllDiscoveryPage(request.getType(),request.getStartIndex(),request.getPageSize());
		User user = null ;
		if (null != request.getUserId() && request.getUserId() > 0) {
			user = userMapper.selectByPrimaryKey(request.getUserId());
		}
//		String url = "http://h5.xzhuang.com/shop/discovery_detail.html?";//生产环境
//		String url = "http://h5.dev.xzhuang.com/shop/discovery_detail.html?";//测试环境
//		String url = "http://h5.dev.xzhuang.com/shop/discovery_detail.html?id=9&t=1212&userId=22";//示例
		BaseRestrictions baseRestrictions = baseRestrictionsMapper.selectAllBaseRestrictionsByType("h5_url");
		if (null == baseRestrictions) {
			logger.error("---------跳转发现详情的H5访问域名为空，请检查数据库基础配置信息表-------");
			throw new RuntimeException("H5访问域名为空，请检查数据库基础配置信息表");
		}
		String url = baseRestrictions.getName();//h5访问地址
		if (!pageList.isEmpty() && null != pageList) {
			for (TitleDiscovery titleDiscovery : pageList) {
				if (null == user) {//不拼接useId
					String u1 = url + "id="+titleDiscovery.getId()+"&t="+System.currentTimeMillis();
					titleDiscovery.setSkipUrl(u1);
				}else {
					String u2 = url + "id="+titleDiscovery.getId()+"&t="+System.currentTimeMillis()+"&userId="+user.getId();
					titleDiscovery.setSkipUrl(u2);
				}
			}
		}
		return pageList;
	}

	@Override
	public int updateDiscovery(Integer id, Integer flag) {
		TitleDiscovery record =  new TitleDiscovery();
		record.setId(id);
		Integer endCount = 0;
		TitleDiscovery titleDiscovery = titleDiscoveryMapper.selectByPrimaryKey(id);
		if (null == titleDiscovery) {
			return 0;
		}
		Integer startCount = titleDiscovery.getLikeTotalCount();
		if ("1".equals(flag.toString())) {//喜欢加1
			endCount = startCount +1;
		}else if("0".equals(flag.toString())){//不喜欢减1
			endCount = startCount -1;
		}else{
			endCount = startCount;
		}
		record.setLikeTotalCount(endCount);
		return titleDiscoveryMapper.updateByPrimaryKeySelective(record);
	}
	//原方法
	/*@Override
	public List<ResponseTitleDiscoveryVo> selectDiscoveryMaterialListPageByType(TitleDiscovery request) {
		Integer storeUserId = this.baseRestrictionsService.getBaseRestrictionsForCache("xz_jj").getExperience();
		User user = null;//登录用户
		if (null != request.getUserId() && request.getUserId()> 0) {
			user = this.userService.getUser(request.getUserId());
			if(user!=null && user.getRecommendFrom()!=null){
				String recommendFrom = user.getRecommendFrom();//登录人对应的店主身份ID
				UserInfo userInfo = this.userService.selectUserInfoByPersionId(recommendFrom);//推荐人身份
				if(userInfo!=null){
					storeUserId = Integer.parseInt(userInfo.getUserId()+"");
				}
			}
		}
		User u = this.userMapper.selectByPrimaryKey(Long.valueOf(storeUserId));//使用推荐人去计算价格（默认校妆姐姐）
		List<ResponseTitleDiscoveryVo> pageList = null;
		if (null != request.getGoodId() && request.getGoodId() != 0 ) {//只查询当前商品对应的素材列表
			pageList = titleDiscoveryMapper.queryAllDiscoveryPageNewByGoodId(request.getType(),request.getGoodId(),request.getStartIndex(), request.getPageSize());
		}else{
			pageList = titleDiscoveryMapper.queryAllDiscoveryPageNew(request.getType(), request.getStartIndex(), request.getPageSize());
		}
		if (null != pageList && !pageList.isEmpty()) {
			for (ResponseTitleDiscoveryVo responseVo : pageList) {
				//商品信息(一个发现素材对应一个商品)
				List<TitleDiscoveryProduct> products = titleDiscoveryProductMapper.selectAll1(responseVo.getId().toString());
				if (null != products && !products.isEmpty()) {
					TitleDiscoveryProduct titleDiscoveryProduct = products.get(0);//一个素材仅包含一个商品
					responseVo.setLongTitle(titleDiscoveryProduct.getContext().replaceAll("<[^>]+>",""));//替换素材展示内容
					GoodsPriceVo goodsPriceVo = new GoodsPriceVo();
//					GoodsPriceVo.setGoodId(Long.parseLong(titleDiscoveryProduct.getGoodsId()));
					if (!StringUtils.isEmpty(titleDiscoveryProduct.getGoodsId()) && !titleDiscoveryProduct.getGoodsId().equals("0")) {
						String[] gId = titleDiscoveryProduct.getGoodsId().split(",");
						for (String id : gId) {
							GoodSerialVo g = this.goodsService.selectOneGoodById(Long.parseLong(id));
//							if (null != g) {//商品未下架才返回
								goodsPriceVo.setGoodId(g.getGoodId());
								goodsPriceVo.setGoodName(g.getName());
								//商品价格
								GoodsPrice goodsViewPriceForProduct = null;
								try {
									goodsViewPriceForProduct = goodsService.getGoodsViewPriceForGoods(u, Long.parseLong(id));
								} catch (Exception e) {
									e.printStackTrace();
								}
								if (null != goodsViewPriceForProduct) {
									goodsPriceVo.setCommission(goodsViewPriceForProduct.getCommission());
									goodsPriceVo.setLowestPrice(goodsViewPriceForProduct.getSalePrice());
									goodsPriceVo.setSuggestedPrice(goodsViewPriceForProduct.getMarketPirce());
								}
//							}
							responseVo.setGoodsPriceVo(goodsPriceVo);
							//拼接分享所需参数
							if (null != user) {//未登录不允许分享
//								Map hashMap = this.addShareGoods(user.getId(), g.getGoodId(), responseVo.getId());
//								if (null != hashMap && !hashMap.isEmpty()) {
//									responseVo.setShareGoodsId(hashMap.get("shareGoodsId").toString());
//									responseVo.setShareUrl(hashMap.get("shareUrl").toString());
//									responseVo.setShareImageUrl(hashMap.get("imageUrl").toString());
//								}
								ResponseEntity<?> responseEntity = shareGoodsService.addShareGoods(user.getId(), g.getGoodId(), responseVo.getId());
								if (null != responseEntity) {
									ResponseResultModel body = (ResponseResultModel) responseEntity.getBody();
									if (null != body) {
										HashMap hashMap = (HashMap)body.getData();
										if (null != hashMap && !hashMap.isEmpty()) {
											responseVo.setShareGoodsId(hashMap.get("shareGoodsId").toString());
											responseVo.setShareUrl(hashMap.get("shareUrl").toString());
											responseVo.setShareImageUrl(hashMap.get("imageUrl").toString());
										}
									}
								}
							}
						}
					}
				}
				//素材标签
				TitleDiscoveryTag titleDiscoveryTag = new TitleDiscoveryTag();
				titleDiscoveryTag.setTitleId(Long.parseLong(responseVo.getId().toString()));
				titleDiscoveryTag.setStatus(1);
				List<TitleDiscoveryTag> tags = titleDiscoveryTagMapper.selectAll(titleDiscoveryTag);
				if (null != tags && !tags.isEmpty()) {
					responseVo.getTitleDiscoveryTagList().addAll(tags);
				}
				//素材图片
				TitleDiscoveryImage titleDiscoveryImage = new TitleDiscoveryImage();
				titleDiscoveryImage.setTitleId(Long.parseLong(responseVo.getId().toString()));
				titleDiscoveryImage.setStatus(1);
				List<TitleDiscoveryImage> images = titleDiscoveryImageMapper.selectAll(titleDiscoveryImage);
				if (null != images && !images.isEmpty()) {
					responseVo.getTitleDiscoveryImageList().addAll(images);
				}
			}
		}
		return pageList;
	}*/
	//批量查询封装素材信息------2018-01-04 修改
	@Override
	public List<ResponseTitleDiscoveryVo> selectDiscoveryMaterialListPageByType(TitleDiscovery request) {
		Integer storeUserId = this.baseRestrictionsService.getBaseRestrictionsForCache("xz_jj").getExperience();
		User user = null;//登录用户
		if (null != request.getUserId() && request.getUserId()> 0) {
//			user = this.userService.getUser(request.getUserId());
			user = this.userService.getUserById(request.getUserId());
			if(user!=null && user.getRecommendFrom()!=null){
				String recommendFrom = user.getRecommendFrom();//登录人对应的店主身份ID
//				UserInfo userInfo = this.userService.selectUserInfoByPersionId(recommendFrom);//推荐人身份
				User userInfo = this.userService.getUserByPersionId(recommendFrom);
				if(userInfo!=null){
					storeUserId = Integer.parseInt(userInfo.getId().toString());
				}
			}
		}
//		User u = this.userMapper.selectByPrimaryKey(Long.valueOf(storeUserId));//使用推荐人去计算价格（默认校妆姐姐）
		User u = this.userService.getUserById(Long.parseLong(storeUserId.toString()));
		List<ResponseTitleDiscoveryVo> pageList = null;
		String nowTime = DateUtils.getNowString(DateUtils.DATE_TIME_FORMAT);
		if (null != request.getGoodId() && request.getGoodId() != 0 ) {//只查询当前商品对应的素材列表
			pageList = titleDiscoveryMapper.queryAllDiscoveryPageNewByGoodId(request.getType(),request.getGoodId(),request.getStartIndex(), request.getPageSize(),nowTime);
		}else{
			pageList = titleDiscoveryMapper.queryAllDiscoveryPageNew(request.getType(), request.getStartIndex(), request.getPageSize(),nowTime);
		}
		List<Integer> titleIds = new ArrayList<>();
		List<Long> goodIds = null;
		if (null != pageList && !pageList.isEmpty()) {
			for (ResponseTitleDiscoveryVo responseVo : pageList) {
				titleIds.add(responseVo.getId());
			}
			//批量查询素材对应的商品
			goodIds = new ArrayList<>();
			Map<String, TitleDiscoveryProduct> mapProducts = new HashMap<>();
			List<TitleDiscoveryProduct> listProducts = titleDiscoveryProductMapper.selectAllBatch(titleIds);
			for (TitleDiscoveryProduct p : listProducts) {
				goodIds.add(Long.parseLong(p.getGoodsId()));
				mapProducts.put(p.getTitleId().toString(), p);
			}
			//批量查询
			//价格
			Map<Long, GoodsPrice> goodsViewPriceForGoods = this.goodsService.getGoodsViewPriceForGoods(u, goodIds);
			//标签
			Map<String, List<TitleDiscoveryTag>> tagListMap = this.queryAllTagsByTitleIdBatch(titleIds);
			//图片
			Map<String, List<TitleDiscoveryImage>> imagListMap = this.queryAllImgsByTitleIdBatch(titleIds);
			BaseRestrictions imageBR = this.baseRestrictionsService.getBaseRestrictionsForCache("image_url");
			String image_url = imageBR.getName();
			//封装返回值
			for (ResponseTitleDiscoveryVo responseVo : pageList) {
				responseVo.setTitle(responseVo.getLongTitle());
				responseVo.setCreateTime(responseVo.getCreateTime().substring(0,10));
				//商品信息(一个发现素材对应一个商品)
				String goodId = null;
				TitleDiscoveryProduct titleDiscoveryProduct = null;
				/*for (TitleDiscoveryProduct p : listProducts) {
					if (responseVo.getId() == p.getTitleId()) {
						goodId = p.getGoodsId();
						titleDiscoveryProduct = p;
						break;
					}
				}*/
				if (!mapProducts.isEmpty()) {
					TitleDiscoveryProduct p = mapProducts.get(responseVo.getId().toString());
					if (p != null) {
						goodId = p.getGoodsId();titleDiscoveryProduct = p;
					}
				}
				if (titleDiscoveryProduct != null) {
					responseVo.setLongTitle(titleDiscoveryProduct.getContext().replaceAll("<[^>]+>","").replaceAll("&nbsp;",""));//替换素材展示内容
					GoodsPriceVo goodsPriceVo = new GoodsPriceVo();
					if (!StringUtils.isEmpty(titleDiscoveryProduct.getGoodsId()) && !titleDiscoveryProduct.getGoodsId().equals("0")) {
						GoodSerialVo g = this.goodsService.selectOneGoodById(Long.parseLong(goodId));
						goodsPriceVo.setGoodId(Long.parseLong(goodId));
						if (null != g) {
							goodsPriceVo.setGoodName(g.getName());
						}
						//商品价格
						GoodsPrice goodsPrice = null;
						if (!goodsViewPriceForGoods.isEmpty()) {
							goodsPrice = goodsViewPriceForGoods.get(Long.parseLong(goodId));
							if (null != goodsPrice) {
								goodsPriceVo.setCommission(goodsPrice.getCommission());
								goodsPriceVo.setLowestPrice(goodsPrice.getSalePrice());
								goodsPriceVo.setSuggestedPrice(goodsPrice.getMarketPirce());
							}
						}
						responseVo.setGoodsPriceVo(goodsPriceVo);
						//拼接分享所需参数（后续可优化为异步查询）
						if (null != user && user.getRoleId()>=3) {//登录且用户角色是店主才有分享
	//								Map hashMap = this.addShareGoods(user.getId(), g.getGoodId(), responseVo.getId());
	//								if (null != hashMap && !hashMap.isEmpty()) {
	//									responseVo.setShareGoodsId(hashMap.get("shareGoodsId").toString());
	//									responseVo.setShareUrl(hashMap.get("shareUrl").toString());
	//									responseVo.setShareImageUrl(hashMap.get("imageUrl").toString());
	//								}
							ResponseEntity<?> responseEntity = shareGoodsService.addShareGoods(user.getId(), Long.parseLong(goodId), responseVo.getId(),"");
							if (null != responseEntity) {
								ResponseResultModel body = (ResponseResultModel) responseEntity.getBody();
								if (null != body) {
									HashMap hashMap = (HashMap)body.getData();
									if (null != hashMap && !hashMap.isEmpty()) {
										responseVo.setShareGoodsId(hashMap.get("shareGoodsId").toString());
										responseVo.setShareUrl(hashMap.get("shareUrl").toString());
										responseVo.setShareImageUrl(hashMap.get("imageUrl").toString().replace(image_url,""));
										if (StringUtils.isBlank(responseVo.getSharePic())) {//分享图片做兼容处理
											responseVo.setSharePic(hashMap.get("imageUrl").toString());
										}
									}
								}
							}
							//商品状态
							Map<String,Object> ltsMap = null;
							if (goodsPrice != null) {
								ltsMap = this.goodsMapper.selectNearlyLimitTimeSale(Long.parseLong(goodId),goodsPrice.getProductId(),nowTime);
							}
							if(ltsMap != null){
								try {
									SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//抢购开始时间
									Date fullBeginTime = sdf.parse(ltsMap.get("fullBeginTime").toString());
									long nowTimeMillis = System.currentTimeMillis();
									// 02-12 12:00
									String fullBeginTime1 = ltsMap.get("fullBeginTime").toString().substring(5, 16);
									String fullEndTime = ltsMap.get("fullEndTime").toString().substring(5, 16);
									if (fullBeginTime.getTime() > nowTimeMillis) {// 预热中
//										String s = "限时特卖02-04 10:00开始";
										String s = fullBeginTime1.substring(0,2)+"月"+fullBeginTime1.substring(3,5)+"日 " +fullBeginTime1.substring(6,11)+"秒杀开始";
										responseVo.setShareGoodStatus(s);
									} else {
//										String s = "限时特卖"+ fullEndTime +"结束";
										String s = fullEndTime.substring(0,2)+"月"+fullEndTime.substring(3,5)+"日 " +fullEndTime.substring(6,11)+"秒杀结束";
										responseVo.setShareGoodStatus(s);
									}
								} catch (Exception e) {
									e.printStackTrace();
								}
							}


						}
					}
				}
				//素材标签
				if (null != tagListMap && !tagListMap.isEmpty()) {
					List<TitleDiscoveryTag> titleDiscoveryTags = tagListMap.get(responseVo.getId().toString());
					if (null != titleDiscoveryTags) {
						responseVo.getTitleDiscoveryTagList().addAll(titleDiscoveryTags);
					}
				}
				//素材图片
				if (null != imagListMap && !imagListMap.isEmpty()) {
					List<TitleDiscoveryImage> images = imagListMap.get(responseVo.getId().toString());
					if (null != images) {
						responseVo.getTitleDiscoveryImageList().addAll(images);
					}
				}
			}
		}
		return pageList;
	}
	/*@Transactional(rollbackFor = Exception.class)
	public Map addShareGoods(long userId, long goodId, long materialId) {
		if (Integer.parseInt(materialId+"") != 0) {// 素材分享，记录分享次数
			this.goodsMapper.saveMaterialOpenAndShareCount(materialId, 0, 1);
		}
		ShareGoods shareGoods = shareGoodsMapper.selectShareGoodsByUserIdAndGoodIdAndAgio(userId, goodId);
		String h5_link = this.baseRestrictionsService.getBaseRestrictionsForCache("h5_link").getName();
		BaseRestrictions imageBR = this.baseRestrictionsService.getBaseRestrictionsForCache("image_url");
		String image_url = imageBR.getName();
		int size = imageBR.getExperience();
		StringBuffer imageUrl = new StringBuffer();
		imageUrl.append(image_url);
		// 商品介绍图
		List<GoodsNavigate> goodsNavigateList = this.goodsNavigateMapper.selectGoodsNavigateByGoodId(goodId);
		if(!CollectionUtils.isEmpty(goodsNavigateList)){
			if(!StringUtil.isEmpty(goodsNavigateList.get(0).getNavigateImage())){
				imageUrl.append(goodsNavigateList.get(0).getNavigateImage());
				imageUrl.append("?imageView2/0/w/");
				imageUrl.append(size);
				imageUrl.append("/h/");
				imageUrl.append(size);
			}
		}
		StringBuffer shareURL = new StringBuffer();
		shareURL.append(h5_link);
		shareURL.append("/goods/index.html");
		Map<String, Object> map = new HashMap<>();
		if (shareGoods == null) {
			shareGoods = new ShareGoods();
			shareGoods.setUserId(userId);
			shareGoods.setGoodId(goodId);
			shareGoodsMapper.insertShareGoods(shareGoods);
			//添加成功 goods表分享次数+1
			this.goodsMapper.saveOpenAndShareCount(Integer.valueOf(goodId+""), 0, 1);
		}
		map.put("shareGoodsId", shareGoods.getId());
		//分享链接 http://h5.xzhuang.com/goods/index.html?talentShareGoodId=1464&talentId=562853&goodId=1464&from=singlemessage
		shareURL.append("?talentShareGoodId=");
		shareURL.append(shareGoods.getId());
		shareURL.append("&goodId=");
		shareURL.append(goodId);
		shareURL.append("&talentId=");
		shareURL.append(userId);
		shareURL.append("&from=singlemessage");
		map.put("shareUrl", shareURL.toString());
		map.put("imageUrl", imageUrl);
		return map;
	}*/
	/*public Long addShareGoods ( long userId, long goodId){
		ShareGoods shareGoods = shareGoodsMapper.selectShareGoodsByUserIdAndGoodIdAndAgio(userId, goodId);
		Long talentShareGoodId = null;
		if (shareGoods == null) {
			shareGoods = new ShareGoods();
			shareGoods.setUserId(userId);
			shareGoods.setGoodId(goodId);
			shareGoodsMapper.insertShareGoods(shareGoods);
			//添加成功 goods表分享次数+1
			this.goodsMapper.saveOpenAndShareCount(Integer.valueOf(goodId + ""), 0, 1);
			talentShareGoodId = shareGoods.getId();
		}
		if (shareGoods != null) {
			talentShareGoodId = shareGoods.getId();
		}
		return talentShareGoodId;
	}*/

	@Override
	public List<DiscoveryTypeVo> findMaxIdsBytype() {
		List<DiscoveryTypeVo> list = titleDiscoveryMapper.findMaxIdsBytype();
		return list;
	}

	@Override
	public int updateDiscoveryCountByid(Integer titleDiscoveryId, Integer type) {
		int i = titleDiscoveryMapper.updateDiscoveryCountByid(titleDiscoveryId,type);
		return i;
	}



	//批量查询素材对应的标签
	@Override
	public Map<String, List<TitleDiscoveryTag>> queryAllTagsByTitleIdBatch(List<Integer> titleIds) {
		List<TitleDiscoveryTag> TitleDiscoveryTagList = titleDiscoveryTagMapper.selectAllBatch(titleIds);
		Map<String, List<TitleDiscoveryTag>> returnMap = null;
		List<TitleDiscoveryTag> list = null;
		if (null != TitleDiscoveryTagList && !TitleDiscoveryTagList.isEmpty()) {
			returnMap = new HashMap<>();
			for (TitleDiscoveryTag titleDiscoveryTag : TitleDiscoveryTagList) {
				if (returnMap.containsKey(titleDiscoveryTag.getTitleId().toString())) {
					returnMap.get(titleDiscoveryTag.getTitleId().toString()).add(titleDiscoveryTag);
				} else {
					list = new ArrayList<>();
					list.add(titleDiscoveryTag);
					returnMap.put(titleDiscoveryTag.getTitleId().toString(),list);
				}
			}
		}
		return returnMap;
	}
	//批量查询素材对应的标签
	@Override
	public Map<String, List<TitleDiscoveryImage>> queryAllImgsByTitleIdBatch(List<Integer> titleIds) {
		List<TitleDiscoveryImage> TitleDiscoveryImageList = titleDiscoveryImageMapper.selectAllBatch(titleIds);
		Map<String, List<TitleDiscoveryImage>> returnMap = null;
		List<TitleDiscoveryImage> list = null;
		if (null != TitleDiscoveryImageList && !TitleDiscoveryImageList.isEmpty()) {
			returnMap = new HashMap<>();
			for (TitleDiscoveryImage titleDiscoveryImage : TitleDiscoveryImageList) {
				if (returnMap.containsKey(titleDiscoveryImage.getTitleId().toString())) {
					returnMap.get(titleDiscoveryImage.getTitleId().toString()).add(titleDiscoveryImage);
				} else {
					list = new ArrayList<>();
					list.add(titleDiscoveryImage);
					returnMap.put(titleDiscoveryImage.getTitleId().toString(),list);
				}
			}
		}
		return returnMap;
	}


}